<template>
    <div class="category-list-page">
        <el-card shadow="hover" class="category-list-card">
            <div class="category-list-toolbar">
                <el-input v-model="keyword" placeholder="搜索分类名" style="width: 200px" @keyup.enter="handleSearch" />
                <el-button type="primary" @click="handleSearch">搜索</el-button>
                <el-button type="primary" @click="handleAdd">新增分类</el-button>
            </div>
            <el-table :data="list" style="width: 100%" stripe border>
                <el-table-column prop="id" label="ID" width="80" />
                <el-table-column prop="name" label="分类名" />
                <el-table-column prop="created_at" label="创建时间">
                    <template #default="scope">
                        {{ formatDate(scope.row.created_at) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template #default="scope">
                        <el-button size="small" @click="handleEdit(scope.row)" class="action-btn">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.row.id)" class="action-btn">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-dialog v-model="dialogVisible" :title="editId ? '编辑分类' : '新增分类'" width="400px" class="category-dialog">
            <el-form :model="editForm" :rules="rules" ref="formRef" label-width="80px">
                <el-form-item label="分类名" prop="name">
                    <el-input v-model="editForm.name" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleSave">保存</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getCategoryList, createCategory, updateCategory, deleteCategory } from '../api/category'

const list = ref<any[]>([])
const keyword = ref('')

const dialogVisible = ref(false)
const editForm = ref<any>({})
const editId = ref<number | null>(null)

const formRef = ref()
const rules = reactive({
    name: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
})

const fetchList = async () => {
    const res = await getCategoryList({ keyword: keyword.value })
    list.value = res.list
}
const handleSearch = () => {
    fetchList()
}
const handleAdd = () => {
    editId.value = null
    editForm.value = {}
    dialogVisible.value = true
}
const handleEdit = (row: any) => {
    editId.value = row.id
    editForm.value = { ...row }
    dialogVisible.value = true
}
const handleSave = async () => {
    await formRef.value.validate(async (valid: boolean) => {
        if (!valid) return
        if (editId.value) {
            await updateCategory(editId.value, editForm.value)
            ElMessage.success('修改成功')
        } else {
            await createCategory(editForm.value)
            ElMessage.success('新增成功')
        }
        dialogVisible.value = false
        fetchList()
    })
}
const handleDelete = (id: number) => {
    ElMessageBox.confirm('确定要删除该分类吗？', '提示', { type: 'warning' })
        .then(async () => {
            await deleteCategory(id)
            ElMessage.success('删除成功')
            fetchList()
        })
}
function formatDate(val: string) {
    if (!val) return ''
    const d = new Date(val)
    return d.toLocaleString()
}
fetchList()
</script>

<style scoped>
.category-list-page {
    padding: 24px;
    background: #fff;
    min-height: 0;
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
}
.category-list-card {
    width: 100%;
    max-width: 100%;
    margin: 0;
    border-radius: 10px;
    box-sizing: border-box;
}
.category-list-toolbar {
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}
.action-btn {
    margin-right: 8px;
}
.category-dialog .el-dialog__body {
    padding: 32px 32px 12px 32px;
    border-radius: 16px;
    background: #f8fafc;
}
</style>
